/*
 * This file is part of the Monero P2Pool <https://github.com/SChernykh/p2pool>
 * Copyright (c) 2021-2024 SChernykh <https://github.com/SChernykh>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include "common.h"
#include "crypto.h"
#include "util.h"
extern "C" {
#include "crypto-ops.h"
}
#include "fcmp_pp_crypto.h"

#include "gtest/gtest.h"
#include <fstream>

namespace p2pool {

TEST(crypto, ops)
{
	init_crypto_cache();
	{
	hash pub, sec;
	generate_keys(pub, sec);
	ASSERT_TRUE(check_keys(pub, sec));

	sec.h[HASH_SIZE - 1] = 0xff;
	ASSERT_FALSE(check_keys(pub, sec));

	// Run the tests several times to check how crypto cache works
	for (int i = 0; i < 4; ++i) {
		if (i == 2) {
			clear_crypto_cache(seconds_since_epoch() - 1);
		}
		else if (i == 3) {
			clear_crypto_cache(seconds_since_epoch() + 1);
		}

		std::ifstream f("crypto_tests.txt");
		ASSERT_EQ(f.good() && f.is_open(), true);
		do {
			std::string name;
			f >> name;
			if (name == "generate_key_derivation") {
				hash key1, key2, derivation, expected_derivation;
				std::string result_str;
				f >> key1 >> key2 >> result_str;
				const bool result = (result_str == "true");
				if (result) {
					f >> expected_derivation;
				}
				uint8_t view_tag;
				ASSERT_EQ(p2pool::generate_key_derivation(key1, key2, 0, derivation, view_tag), result);
				ASSERT_EQ(p2pool::generate_key_derivation(key1, key2, 1, derivation, view_tag), result);
				ASSERT_EQ(p2pool::generate_key_derivation(key1, key2, 2, derivation, view_tag), result);
				ASSERT_EQ(p2pool::generate_key_derivation(key1, key2, 3, derivation, view_tag), result);
				if (result) {
					ASSERT_EQ(derivation, expected_derivation);
				}
			}
			else if (name == "derive_public_key") {
				hash derivation, base, derived_key, expected_derived_key;
				std::string result_str;
				size_t output_index;
				f >> derivation >> output_index >> base >> result_str;
				const bool result = (result_str == "true");
				if (result) {
					f >> expected_derived_key;
				}
				ASSERT_EQ(derive_public_key(derivation, output_index, base, derived_key), result);
				if (result) {
					ASSERT_EQ(derived_key, expected_derived_key);
				}
			}
			else if (name == "derive_view_tag") {
				hash derivation;
				uint64_t output_index;
				std::string result_str;
				f >> derivation >> output_index >> result_str;
				uint8_t view_tag;
				p2pool::derive_view_tag(derivation, output_index, view_tag);

				char buf[log::Stream::BUF_SIZE + 1];
				log::Stream s(buf);
				s << log::hex_buf(&view_tag, 1) << '\0';

				ASSERT_EQ(buf, result_str);
			}
			else if (name == "get_tx_keys") {
				hash wallet_spend_key, monero_block_id, pub_check, sec_check;
				f >> wallet_spend_key >> monero_block_id >> pub_check >> sec_check;

				hash pub, sec;
				p2pool::get_tx_keys(pub, sec, wallet_spend_key, monero_block_id);

				ASSERT_EQ(pub, pub_check);
				ASSERT_EQ(sec, sec_check);
			}
			else if (name == "check_key") {
				hash pub_key;
				std::string result_str;

				f >> pub_key >> result_str;				

				ge_p3 p;
				ASSERT_EQ(ge_frombytes_vartime(&p, pub_key.h) == 0, result_str == "true");
			}
			else if (name == "check_torsion") {
				hash pub_key;
				std::string result_str;

				f >> pub_key >> result_str;				

				ge_p3 p;
				ASSERT_EQ(
					(ge_frombytes_vartime(&p, pub_key.h) == 0)
					&& !fcmp_pp::mul8_is_identity(p)
					&& fcmp_pp::torsion_check_vartime(p)
				, result_str == "true");
			}
		} while (!f.eof());
	}
	}
	clear_crypto_cache(0);
	destroy_crypto_cache();

#ifdef WITH_INDEXED_HASHES
	indexed_hash::cleanup_storage();
#endif
}

}
